home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / src / SmplHist.cc < prev    next >
C/C++ Source or Header  |  1992-10-27  |  3KB  |  113 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Dirk Grunwald (grunwald@cs.uiuc.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #ifdef __GNUG__
  19. #pragma implementation
  20. #endif
  21. #include <stream.h>
  22. #include <SmplHist.h>
  23. #include <math.h>
  24.  
  25. #ifndef HUGE_VAL
  26. #ifdef HUGE
  27. #define HUGE_VAL HUGE
  28. #else
  29. #include <float.h>
  30. #define HUGE_VAL DBL_MAX
  31. #endif
  32. #endif
  33.  
  34. const int SampleHistogramMinimum = -2;
  35. const int SampleHistogramMaximum = -1;
  36.  
  37. SampleHistogram::SampleHistogram(double low, double high, double width)
  38. {
  39.     if (high < low) {
  40.     double t = high;
  41.     high = low;
  42.     low = t;
  43.     }
  44.  
  45.     if (width == -1) {
  46.     width = (high - low) / 10;
  47.     }
  48.  
  49.     howManyBuckets = int((high - low) / width) + 2;
  50.     bucketCount = new int[howManyBuckets];
  51.     bucketLimit = new double[howManyBuckets];
  52.     double lim = low;
  53.     for (int i = 0; i < howManyBuckets; i++) {
  54.     bucketCount[i] = 0;
  55.     bucketLimit[i] = lim;
  56.     lim += width;
  57.     }
  58.     bucketLimit[howManyBuckets-1] = HUGE_VAL;    /* from math.h */
  59. }
  60.  
  61. SampleHistogram::~SampleHistogram()
  62. {
  63.     if (howManyBuckets > 0) {
  64.     delete bucketCount;
  65.     delete bucketLimit;
  66.     }
  67. }
  68.  
  69. void
  70. SampleHistogram::operator+=(double value)
  71. {
  72.     int i;
  73.     for (i = 0; i < howManyBuckets; i++) {
  74.     if (value < bucketLimit[i]) break;
  75.     }
  76.     bucketCount[i]++;
  77.     this->SampleStatistic::operator+=(value);
  78. }
  79.  
  80. int
  81. SampleHistogram::similarSamples(double d)
  82. {
  83.     int i;
  84.     for (i = 0; i < howManyBuckets; i++) {
  85.     if (d < bucketLimit[i]) return(bucketCount[i]);
  86.     }
  87.     return(0);
  88. }
  89.  
  90. void
  91. SampleHistogram::printBuckets(ostream& s)
  92. {
  93.     for(int i = 0; i < howManyBuckets; i++) {
  94.     if (bucketLimit[i] >= HUGE_VAL) {
  95.         s << "< max : " << bucketCount[i] << "\n";
  96.     } else {
  97.         s << "< " << bucketLimit[i] << " : " << bucketCount[i] << "\n";
  98.     }
  99.     }
  100. }
  101.  
  102. void
  103. SampleHistogram::reset()
  104. {
  105.     this->SampleStatistic::reset();
  106.     if (howManyBuckets > 0) {
  107.     for (register int i = 0; i < howManyBuckets; i++) {
  108.         bucketCount[i] = 0;
  109.     }
  110.     }
  111. }
  112.  
  113.